package com.shuyun.stockdata.utils;

import com.shuyun.stockdata.pojo.Data;
import com.shuyun.stockdata.pojo.TimesHttp;

import java.io.*;
import java.util.Map;
import java.util.Properties;

/**
 * 文件处理工具类
 * Created by sw on 2018/3/14
 */
public class HandleFilleUtil {


    /**
     * 根据key读取value
     *
     * @param filePath 绝对路径或相对路径， 如果是相对路径，则从当前项目下的目录开始计算，
     *                 如：当前项目/configure/configure.properties,
     *                 相对路径就是configure/configure.properties
     * @param key
     * @return
     */
    public static String readValue(String filePath, String key) {
        Properties props = new Properties();
        String value = null;
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            value = props.getProperty(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    /**
     * 根据日期获取数据
     *
     * @param timesHttpMap
     * @param metaData
     * @param queryDate    具体日期
     * @return
     */

    public static StringBuilder getDataByDate(Map<String, TimesHttp> timesHttpMap, String metaData, String queryDate) {

        StringBuilder stringBuilder = new StringBuilder();

        TimesHttp timesHttp = timesHttpMap.get(queryDate);
        if (timesHttp == null || "".equals(timesHttp)) {
            System.out.println("股票："+metaData+"\t"+ queryDate + "天没有数据");
        } else {
            stringBuilder.append(metaData)
                    .append(",")
                    .append(queryDate)
                    .append(",")
                    .append(timesHttp.getOpen())
                    .append(",")
                    .append(timesHttp.getHigh())
                    .append(",")
                    .append(timesHttp.getLow())
                    .append(",")
                    .append(timesHttp.getClose())
                    .append(",")
                    .append(timesHttp.getVolumne());
        }

        return stringBuilder;
    }

    /**
     * 获取全部数据
     *
     * @param timestamp 时间
     * @param timesHttp 股票数据对象
     * @param data      全部数据
     * @return
     */
    public static StringBuilder getDataAll(String timestamp, TimesHttp timesHttp, Data data) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(data.getMetaData().getSymbol())
                .append(",")
                .append(timestamp)
                .append(",")
                .append(timesHttp.getOpen())
                .append(",")
                .append(timesHttp.getHigh())
                .append(",")
                .append(timesHttp.getLow())
                .append(",")
                .append(timesHttp.getClose())
                .append(",")
                .append(timesHttp.getVolumne());
        return stringBuilder;
    }

    /**
     * 输出数据到本地文件
     *
     * @param builder
     * @param localPath
     */

    public static void writeData(StringBuilder builder, String localPath) throws IOException {

        RandomAccessFile randomFile = null;
        try {
            // 打开一个随机访问文件流，按读写方式
            randomFile = new RandomAccessFile(localPath, "rw");
            // 文件长度，字节数
            long fileLength = randomFile.length();
            // 将写文件指针移到文件尾。
            randomFile.seek(fileLength);
            randomFile.writeBytes(builder.toString() + "\r\n");
            randomFile.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            randomFile.close();
        }
    }
}
